//***************************************

//****************************************

#include "I2C.h"
#include "CHT8305.h"
#include "delay.h"
#include "rs485.h"

// unsigned char TH_Init()
// {
// 	char g_addr = 0xfe; //´Ó»úµØÖ·
// 	char restbuf = 0x01;
// 	WriteNbyte(0x00, &restbuf, 1);

// 	return 0; //
// }

// unsigned char TH_Read(AM2321_Data_TypeDef *AM2321_Data)
// {
// 	char ret;
// 	char g_addr = 0xfe; //´Ó»úµØÖ·
// 	char g_wbuf = 0x25;

// 	short humi;
// 	short temp;
// 	char t_addr_l = 0x06;
// 	char h_addr_l = 0x04;

// 	WriteNbyte(0x01, (unsigned char *)&g_wbuf, 1); //ÎÂÊª¶È²Ù×÷ÅäÖÃ
// 	delay_ms(1);
// 	ReadNbyte(h_addr_l, (unsigned char *)&humi, 2); //¶Á³ö2¸ö×Ö½ÚÊª¶ÈÊý¾Ý
// 	humi = humi * 1000 / 1024;
// 	delay_ms(1);
// 	ReadNbyte(t_addr_l, (unsigned char *)&temp, 2); //¶Á³ö2¸ö×Ö½ÚÎÂ¶ÈÊý¾Ý
// 	temp = temp - 774;
// 	if (humi > 999)
// 		humi = 999;
// 	AM2321_Data->humi_int = humi / 10;
// 	AM2321_Data->humi_deci = humi % 10;
// 	AM2321_Data->temp_int = temp / 10;
// 	AM2321_Data->temp_deci = temp % 10;

// 	return ret;
// }

unsigned char TH_CHT8305(AM2321_Data_TypeDef *AM2321_Data)
{
	char g_addr = 0x80;
	unsigned short tmpa = 0, tmpb = 0;
	float tempa = 0, humia = 0;
	char ret;

	unsigned char buff[4] = {0};

	ReadNbyte(0x00, (unsigned char *)&buff, 4);
	tmpa = (buff[0] * 256) + buff[1];
	tmpb = (buff[2] * 256) + buff[3];

	tempa = (tmpa * 165.0) / 65535.0 - 40; //ʵ�ʵ���ʪ��
	humia = tmpb / 655.35;
	// printf("%f  %f \r\n", tempa, humia);

	AM2321_Data->humi_int = humia;
	AM2321_Data->humi_deci = (int)(humia * 10) % 10;
	AM2321_Data->temp_int = tempa;
	AM2321_Data->temp_deci = (int)(tempa * 10) % 10;

	return ret;
}
